#include "frmusermodify.h"
#include "ui_frmusermodify.h"
#include "userinfoservice.h"
#include <quuid.h>

FrmUserModify::FrmUserModify(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FrmUserModify)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::WindowCloseButtonHint);
    service=new UserInfoService;
    this->userType=0;
}

FrmUserModify::~FrmUserModify()
{
    delete ui;
}

void FrmUserModify::setUserGuid(QString userGUID)
{
    this->userGUID=userGUID;
}
void FrmUserModify::setUserName(QString userName)
{
    this->userName=userName;
    ui->txtUserName->setText(userName);
}
void FrmUserModify::setRealName(QString realName)
{
    this->realName=realName;
    ui->txtRealName->setText(realName);
}
void FrmUserModify::setDepartment(QString department)
{
    this->department=department;
    ui->txtDepartment->setText(department);
}
void FrmUserModify::setUserType(int userType)
{
    this->userType=userType;
    if(userType==-1)
    {
        ui->rbtAdmin->setChecked(true);
    }
    else
    {
        ui->rbtUnAdmin->setChecked(true);
    }
}

void FrmUserModify::on_btnConfirm_clicked()
{
    bool isEdit=true;
    QMap<QString,QString> map;
    if(this->userGUID.isEmpty())
    {
        isEdit=false;
        QUuid quid=QUuid::createUuid();
        this->userGUID=quid.toString();
        this->userGUID= this->userGUID.mid(1,this->userGUID.length()-2);
        map.insert("pwd","123456");
    }


    map.insert("username",ui->txtUserName->text());
    map.insert("realname",ui->txtRealName->text());
    map.insert("department",ui->txtDepartment->text());
    map.insert("usertype",QString("%1").arg(this->userType));
    map.insert("userguid",this->userGUID);

    bool blResult;
    if(isEdit)
    {
        blResult= service->update(map);
    }
    else
    {

        blResult= service->insert(map);
    }
    if(blResult)
    {
        emit sendData();
        this->close();
    }
}

void FrmUserModify::on_btnCancel_clicked()
{
    this->close();
}

void FrmUserModify::on_rbtUnAdmin_toggled(bool checked)
{
    this->userType=checked?0:-1;
}

void FrmUserModify::on_rbtAdmin_toggled(bool checked)
{
    this->userType=checked?-1:0;
}
